﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication4.Models;
using Syncfusion.DocIO.DLS;
using Syncfusion.Mvc.Grid;
using Syncfusion.Pdf;
using Syncfusion.XlsIO;

namespace MvcApplication4.Controllers
{
    public class CadastroVacinaController : Controller
    {
        private BD_Vacinas2Context db = new BD_Vacinas2Context();

        //
        // GET: /CadastroVacina/

        public ActionResult Index()
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Vacina.Take(100).ToList();
            return View(data);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(PagingParams args)
        {
            IEnumerable data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Vacina.Take(100).ToList();
            return data.GridActions<tb_Vacina>();
        }

        public ActionResult ExportToExcel()
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Vacina.Take(200).ToList();
            return data.GridExportToExcel<tb_Vacina>("GridExcel.xlsx", ExcelVersion.Excel2010);
        }

        public ActionResult ExportToWord(PagingParams args)
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Vacina.Take(200).ToList();
            return data.GridExportToWord<tb_Vacina>("GridWord.doc", DocumentVersion.Word2007, args.ExportOption);
        }

        public ActionResult ExportToPDF(PagingParams args)
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Vacina.Take(200).ToList();
            return data.GridExportToPDF<tb_Vacina>("GridPDF.pdf", PdfVersion.Version1_7, args.ExportOption);
        }

        //
        // GET: /CadastroVacina/Details/5

        public ActionResult Details(int id = 0)
        {
            tb_Vacina tb_vacina = db.tb_Vacina.Find(id);
            if (tb_vacina == null)
            {
                return HttpNotFound();
            }
            return View(tb_vacina);
        }

        //
        // GET: /CadastroVacina/Create

        public ActionResult Create()
        {
            List<SelectListItem> tipo = new List<SelectListItem>();
            tipo.Add(new SelectListItem { Text = "OBRIGATÓRIA", Selected = true });
            tipo.Add(new SelectListItem { Text = "NÃO-OBRIGATÓRIA" });
            ViewBag.Tipo = tipo;
            return View();
        }

        [HttpPost]
        public JsonResult doesVacinaExist(tb_Vacina tb_vacina)
        {

            var vac = from f in db.tb_Vacina
                       select f;

            vac = vac.Where(o => o.Nome_Vacina.Equals(tb_vacina.Nome_Vacina));

            return Json(!vac.Any());
        }

        //
        // POST: /CadastroVacina/Create

        [HttpPost]
        public ActionResult Create(tb_Vacina tb_vacina)
        {
            if (ModelState.IsValid)
            {
                db.tb_Vacina.Add(tb_vacina);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tb_vacina);
        }

        //
        // GET: /CadastroVacina/Edit/5

        public ActionResult Edit(int id = 0)
        {
            tb_Vacina tb_vacina = db.tb_Vacina.Find(id);
            if (tb_vacina == null)
            {
                return HttpNotFound();
            }

            List<SelectListItem> tipo = new List<SelectListItem>();
            tipo.Add(new SelectListItem { Text = "OBRIGATÓRIA", Selected = true });
            tipo.Add(new SelectListItem { Text = "NÃO-OBRIGATÓRIA" });
            ViewBag.Tipo = tipo;

            return View(tb_vacina);
        }

        //
        // POST: /CadastroVacina/Edit/5

        [HttpPost]
        public ActionResult Edit(tb_Vacina tb_vacina)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_vacina).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tb_vacina);
        }

        //
        // GET: /CadastroVacina/Delete/5

        public ActionResult Delete(int id = 0)
        {
            tb_Vacina tb_vacina = db.tb_Vacina.Find(id);
            if (tb_vacina == null)
            {
                return HttpNotFound();
            }
            return View(tb_vacina);
        }

        //
        // POST: /CadastroVacina/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            tb_Vacina tb_vacina = db.tb_Vacina.Find(id);
            db.tb_Vacina.Remove(tb_vacina);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}